//获取应用实例
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingMoreHidden:false,
    active:1,
    lists:[],
    page:1,
    type:'',
    filter:[{label:'',name:"全部"},{label:1,name:"社交电商"},{label:2,name:"直播订单"},{label:3,name:"社区团购"}],
    order_status:'',
    status:[{label:'',name:"全部"},{label:1,name:"待付款"},{label:2,name:"待成团"},{label:3,name:"待发货"},{label:4,name:"已完成"}],
    day:'-1',
    days:[{label:'-1',name:"全部"},{label:0,name:"今日"},{label:7,name:"7天"},{label:30,name:"1个月"},{label:365,name:"1年内"}],
    border_status:"",
    bStatus:[{label:'',name:"全部"},{label:'after_sale_waiting_return',name:"申请中"},{label:"after_sale_return_accept",name:"审核通过"},{label:'after_sale_return_refuse',name:"申请拒绝"},{label:'after_sale_return_cancel',name:"取消"},{label:'after_sale_return_dispatch',name:"已发货"},{label:'after_sale_return_admin_received',name:"已收货"},{label:'after_sale_return_refund',name:"已退款"},{label:'after_sale_return_admin_dispatch',name:"商家已发货"}],
  },
  // 更改筛选项
  changeFilter(e){
    let item= e.currentTarget.dataset.item;
    this.setData({
      page:1,
      lists:[],
      type:item.label
    })
    this.getDataLists();
  },
  changeStatus(e){
    let item= e.currentTarget.dataset.item;
    this.setData({
      page:1,
      lists:[],
      order_status:item.label
    })
    this.getDataLists();
  },
  changeBStatus(e){
    let item= e.currentTarget.dataset.item;
    this.setData({
      page:1,
      lists:[],
      border_status:item.label
    })
    this.getDataLists();
  },
  changeDays(e){
    let item= e.currentTarget.dataset.item;
    this.setData({
      page:1,
      lists:[],
      day:item.label
    })
    this.getDataLists();
  },
  // 切换tab
  changeType(e){
    let type= e.currentTarget.dataset.type;
    this.setData({
      page:1,
      lists:[],
      active:type
    })
    this.getDataLists();
  },
  
  // 详情
  goodsEdit(e){
    let item= e.currentTarget.dataset.item;
    console.log("点击查看详情",item)
    
    wx.navigateTo({
      url: '/pages/order-detail/order-detail?id='+(item.order_id||item.id)+'&type=m&b='+this.data.b+"&show="+(this.data.active==2),
    })
  },
  // 确认收货
  confrimGoods(e){
    var that = this;
    let item= e.currentTarget.dataset.item;
    wx.showModal({
      title:"是否确定收到货物",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/confirm-deliver',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            id:item.id
          },
          success: function (res) {
            if(res.data.code==200){
              wx.showToast({
                title: '确认收货成功',
                icon:'none'
              })
              that.setData({
                p:1,
                lists:[]
              })
              that.getDataLists();
            }else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
            
          }
        })
      }
    })
  },
  // 去换货
  changeGoods(e){
    let item= e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=5&title=去换货&id="+item.increment_id+'&yellow=0&bid='+item.id+"&status_text="+item.status
    })
  },
  // 拒绝换货
  rejectChange(e){
    let item= e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=5&success=true&title=换货申请&id="+item.id+"&status_text="+item.status
    })
  },
  // 同意退款
  confrimBackMoney(e){
    var that = this;
    let item= e.currentTarget.dataset.item;
    wx.showModal({
      title:"是否确定退款",
      success:function(res){
        if(res.confirm){
          wx.request({
            url: app.globalData.urls + '/order-manage/after-pay',
            header: app.getRequestHeader(),
            method: 'POST',
            data: {
              id:item.id
            },
            success: function (res) {
              if(res.data.code==200){
                wx.navigateTo({
                  url: "/pages/apply-for-return/apply-for-return?type=4&title=同意退款&status=已同意退款，等待微信退款"
                })
              }else{
                wx.showToast({
                  title: res.data.message,
                  icon:'none'
                })
              }
              
            }
          })
        }
      }
    })
    
  },
  // 拒绝退货退款
  rejectChangeReturn(e){
    let item= e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=6&status=5&title=退货退款申请&id="+item.id+"&status_text="+item.status
    })
  },
  // 同意退货退款
  resolveChangeReturn(e){
    let item= e.currentTarget.dataset.item,type= e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=6&title="+(type||'退货退款申请')+"&id="+item.increment_id+"&orderId="+item.id+"&status_text="+item.status
    })
  },
  // 获取列表
  getDataLists(){
    var that = this;
    if(this.data.b==1){
      wx.request({
        url: app.globalData.urls + '/order-manage/after-list',
        header: app.getRequestHeader(),
        method: 'POST',
        data: {
          status:this.data.border_status,
          p:this.data.page,
          bdmin_user_id:util.getBdmin_user_id()
        },
        success: function (res) {
          wx.hideLoading();
          let data  = res.data.data
          console.log("-----售后列表-----",data)
          data.forEach(val=>{
            switch(val.status){
                case "after_sale_waiting_return":
                  val.status_text = "申请中"
                break;
                case "after_sale_return_accept":
                  val.status_text = "审核通过"
                break;
                case "after_sale_return_refuse":
                  val.status_text = "申请拒绝"
                break;
                case "after_sale_return_cancel":
                  val.status_text = "取消"
                break;
                case "after_sale_return_dispatch":
                  val.status_text = "已发货"
                break;
                case "after_sale_return_received":
                  val.status_text = "已收货"
                break;
                case "after_sale_return_refund":
                  val.status_text = "已退款"
                break;
                case "after_sale_return_admin_dispatch":
                  val.status_text = "商家已发货"
                break;
                case "after_sale_return_admin_received":
                  val.status_text = "商家已收货"
                break;
            }
          })
          let lists = that.data.lists.concat(data)
          that.setData({
            lists: lists,
            loadingMoreHidden:data && data.length==0 || !data
          });
          app.saveReponseHeader(res);
        }
      })
    }else{
      wx.request({
        url: app.globalData.urls + '/order-manage/list',
        header: app.getRequestHeader(),
        method: 'POST',
        data: {
          category:this.data.active,
          type:this.data.type,
          order_status:this.data.order_status,
          day:this.data.day,
          p:this.data.page,
          bdmin_user_id:util.getBdmin_user_id()
        },
        success: function (res) {
          wx.hideLoading();
          let data  = res.data.data
          data.forEach(val=>{
            switch(val.order_status){
                case "payment_pending":
                  val.status = "待支付"
                break;
                case "payment_confirmed":
                  val.status = "待发货"
                break;
                case "dispatched":
                  val.status = "已发货"
                break;
                case "completed":
                  val.status = "已完成"
                break;
                case "order_received":
                  val.status = "已收货"
                break;
                case "payment_canceled":
                  val.status = "已取消"
                break;
                case "holded":
                  val.status = "待审核"
                break;
                case "cancle":
                  val.status = "已退款已取消"
                break;
            }
          })
          let lists = that.data.lists.concat(data)
          console.log("数据",data)
          that.setData({
            lists: lists,
            loadingMoreHidden:data && data.length==0 || !data
          });
          app.saveReponseHeader(res);
        }
      })
    }
  },
  // 同意退款
  confrimReturn(e){
    let item = e.currentTarget.dataset.item,index = e.currentTarget.dataset.index;
    item.order_status = "";
    var that = this;
    wx.showModal({
      title:"是否确定退款",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/pay',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            order_id:item.order_id,
          },
          success: function (res) {
            console.log('---退款---',res.data.data)
            if(res.data.code==200){
              wx.showToast({
                title: '退款成功',
                icon:'none'
              })
              that.setData({
                page:1,
                ["lists["+index+"]"]:item
              })
            }else{
              wx.showToast({
                title: res.data.message,
                icon:'none'
              })
            }
          }
        })
      }
    })
  },
  // 取消订单
  deleteOrder(e){
    let item = e.currentTarget.dataset.item;
    var that = this;
    wx.showModal({
      title:"是否取消订单",
      success:function(){
        wx.request({
          url: app.globalData.urls + '/order-manage/cancel',
          header: app.getRequestHeader(),
          method: 'POST',
          data: {
            order_id:item.order_id
          },
          success: function (res) {
            console.log('---取消订单---',res.data.data)
            if(res.data.code==200){
              wx.showToast({
                title: '删除成功',
                icon:'none'
              })
              that.setData({
                page:1,
                lists:[]
              })
              that.getDataLists();
            }else{
              wx.showToast({
                title: '取消失败',
                icon:'none'
              })
            }
          }
        })
      }
    })
    console.log("e-------------",item)
  },
  /**
   * 生命周期函数--监听页面加载
   * b=1 是否是退换售后
   */
  onLoad: function (options) {
    
    this.setData({
      b:options.b,
      day:isNaN(Number(options.day)) ? '-1' : Number(options.day),
      type: isNaN(Number(options.type)) ? "" : Number(options.type)
    })
    console.log("-------",this.data.lists)
    // this.getDataLists();
  },
  // 发货
  sendGoods(e){
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=3&title=发货&info=" + JSON.stringify(item)
    })
  },
  // 退款
  returnOrder(){
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: "/pages/apply-for-return/apply-for-return?type=4&title=退款申请&info=" + JSON.stringify(item)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lists:[],
      page:1
    })
    this.getDataLists();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onReachBottom: function () {
    if(!this.data.loadingMoreHidden){
      this.setData({
        page:Number(this.data.page)+1
      })
      this.getDataLists();
    }
  },
})